#pragma once
#include <thread>
#include <functional>
#include <condition_variable>
#include <vector>
#include <mutex>
#include <atomic>

//基于CTRP实现单例模式
#include "Singleton.h"
#include "TaskQueue.h"

class MyTask;

class ThreadPool:public Singleton<ThreadPool>
{
    friend Singleton<ThreadPool>;
private:
    std::condition_variable _cond;
    std::vector<std::thread> _threads;
    //存放任务的队列
    TaskQueue _taskQueue;
    std::mutex _mutex;//用于保护_taskQueue

    std::atomic<bool> _isStop;
    

private:
    ThreadPool(int threadNum=std::thread::hardware_concurrency());
    std::shared_ptr<MyTask> getTask();
    void doTask();
    
public:
    ~ThreadPool();
    void addTask(std::shared_ptr<MyTask> task);
    void start();
    void stop();
};